import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:x_core/x_core.dart';
import 'package:x_project/res/colors.dart';

/// Created by XieXin on 2020/5/27.
/// item输入框
class XItemTextField extends StatefulWidget {
  /// 用于集成测试寻找widget
  final String? keyName;
  final String title;
  final TextEditingController? controller;
  final double? fontSize;
  final int maxLength;
  final bool autoFocus;
  final TextInputType? keyboardType;
  final String? hintText;
  final FocusNode? focusNode;
  final bool isShowClear;
  final double? height;
  final EdgeInsetsGeometry? padding;
  final Color backgroundColor;
  final bool enabled;

  const XItemTextField({
    Key? key,
    this.keyName,
    required this.title,
    this.controller,
    this.fontSize,
    this.maxLength = 32,
    this.autoFocus = false,
    this.keyboardType,
    this.hintText,
    this.focusNode,
    this.isShowClear = false,
    this.height,
    this.backgroundColor = Colors.transparent,
    this.padding,
    this.enabled = true,
  }) : super(key: key);

  @override
  XItemTextFieldState createState() => XItemTextFieldState();
}

class XItemTextFieldState extends State<XItemTextField> {
  late bool _isShowClear;

  @override
  void initState() {
    initClear();

    ///监听输入改变
    widget.controller?.addListener(() {
      setState(() {
        initClear();
      });
    });
    super.initState();
  }

  void initClear() {
    ///获取初始化值
    _isShowClear = widget.controller?.text.isNotEmpty ?? false;

    ///外部控制是否显示
    if (_isShowClear) _isShowClear = widget.isShowClear;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      height: widget.height ?? 72.h,
      padding: widget.padding ?? EdgeInsets.symmetric(horizontal: 32.w),
      color: widget.backgroundColor,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          _getText(),
          Expanded(
            child: _getTextField(),
          ),
          SafeArea(
            right: true,
            child: _getClearButton(),
          ),
        ],
      ),
    );
  }

  ///获取文本
  Widget _getText() {
    return Container(
      margin: EdgeInsets.only(right: 10.w),
      child: Text(
        widget.title,
        style: TextStyle(
          fontSize: widget.fontSize ?? 28.sp,
          color: XColors.textColor33,
        ),
      ),
    );
  }

  ///获取输入框
  Widget _getTextField() {
    return TextField(
      enabled: widget.enabled,
      style: TextStyle(fontSize: widget.fontSize ?? 28.sp),
      focusNode: widget.focusNode,
      maxLength: widget.maxLength,
      autofocus: widget.autoFocus,
      controller: widget.controller,
      keyboardType: widget.keyboardType,
      inputFormatters: _getInputFormatters(),
      decoration: InputDecoration(
        counterText: "",
        contentPadding: const EdgeInsets.all(0),
        hintText: widget.hintText,
        border: InputBorder.none,
      ),
    );
  }

  _getInputFormatters() {
    if (widget.keyboardType ==
        const TextInputType.numberWithOptions(decimal: true)) {
      return <TextInputFormatter>[PrecisionLimitFormatter()];
    }
    if (widget.keyboardType == TextInputType.number ||
        widget.keyboardType == TextInputType.phone) {
      return <TextInputFormatter>[FilteringTextInputFormatter.digitsOnly];
    }
    return null;
  }

  ///获取清除按钮
  Widget _getClearButton() {
    return _isShowClear
        ? Semantics(
            label: "清空",
            hint: "清空输入框",
            child: InkWell(
              onTap: () {
                setState(() {
                  widget.controller!.text = "";
                });
              },
              child: Icon(
                const IconData(0xe781, fontFamily: "Icons"),
                size: 30.w,
                color: XColors.textColorB1,
              ),
            ),
          )
        : const SizedBox.shrink();
  }

  @override
  void dispose() {
    widget.controller?.removeListener(() {});
    super.dispose();
  }
}
